Option Strict Off
Option Explicit On
Namespace BMLERP
	Friend Class frmINV130
		Inherits System.Windows.Forms.Form
		Dim OldTPTYP As String
		Private Sub WriteToFile(ByRef tri As Byte)
			Dim cmdExecute As Object
			Dim SQLInsert As String
			If tri = 0 Then
				SQLInsert = "INSERT INTO ITP " & "(TPTYP,TPDSC,TPCCD) " & "VALUES " & "('" & txtTPTYP.Text & "', '" & txtTPDSC.Text & "', '" & txtTPCCD.Text & "')"
			Else
				SQLInsert = "UPDATE ITP SET TPTYP = '" & txtTPTYP.Text & "', TPDSC ='" & txtTPDSC.Text & "', " & "TPCCD ='" & txtTPCCD.Text & "' WHERE TPTYP ='" & OldTPTYP & "'"


			End If

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQLInsert
			cmdExecute.Execute()

		End Sub

		Private Sub WriteToScreen(ByRef tri As Byte)
			Dim TBL As ADODB.Recordset
			If tri = 0 Then	'gan rong
				txtTPTYP.Text = ""
				txtTPDSC.Text = ""
				txtTPCCD.Text = ""

			Else
				TBL = New ADODB.Recordset
				TBL.Open("  Select ITP.*From ITP where  TPTYP ='" & cboTPTYP.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtTPTYP.Text = IIf(IsDBNull(TBL.Fields("tptyp").Value), "", TBL.Fields("tptyp").Value)
					txtTPDSC.Text = IIf(IsDBNull(TBL.Fields("TPDSC").Value), "", TBL.Fields("TPDSC").Value)
					txtTPCCD.Text = IIf(IsDBNull(TBL.Fields("tpccd").Value), "", TBL.Fields("tpccd").Value)
				Else
					cboTPTYP.BoundText = ""
					txtTPTYP.Text = ""
					txtTPDSC.Text = ""
					txtTPCCD.Text = ""
				End If
				TBL.Close()
			End If
			OldTPTYP = txtTPTYP.Text
		End Sub

		Private Sub Control(ByRef GT As Object)

			txtTPTYP.Enabled = GT
			txtTPDSC.Enabled = GT
			txtTPCCD.Enabled = GT



			txtTPTYP.ReadOnly = Not GT
			txtTPDSC.ReadOnly = Not GT
			txtTPCCD.ReadOnly = Not GT


			cboTPTYP.Enabled = Not GT
			cboTPTYP.Locked = GT
		End Sub

		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			'txtIMITN.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub

		Private Sub cboTPTYP_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTPTYP.ClickEvent
			If eventArgs.area = 2 Then
				WriteToScreen((1))
			End If

		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen((0))
			Control((True))
			' Ham Gan tri man hinh
			ButtonStatus()

		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False
			Control((False))
			ButtonStatus()
			txtTPTYP.Text = cboTPTYP.BoundText
			WriteToScreen((1))

		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			Dim SQL As String
			SQL = "DELETE FROM ITP WHERE TPTYP = '" & cboTPTYP.BoundText & "'"

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQL
			cmdExecute.Execute()
			Adodc1.Refresh()
			WriteToScreen((0))

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True
			Control((True))

			ButtonStatus()


		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			' Ham Save tri vao file
			'Dim tbl_ChapNhan As ADODB.Recordset
			'Set tbl_ChapNhan = New ADODB.Recordset
			'tbl_ChapNhan.Open "  Select IIM.*From IIM ", DBconn, adOpenKeyset, adLockOptimistic, adCmdText
			'tbl_ChapNhan.Close
			If ISADDING = False Then
				WriteToFile((1))
			Else
				WriteToFile((0))
			End If
			Adodc1.Refresh()
			Control((False))
			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()

		End Sub

		Private Sub frmINV130_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			If Not InitSuccess Then
				BIENCHUNG()
			End If

			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "ITP"	'Item type
			Adodc1.Refresh()

			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			Me.VB6_AddADODataBinding()
			Me.cboTPTYP.RowSource = Me.ADOBind_Adodc1.DataSource

		End Sub

		Private Sub txtTPTYP_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTPTYP.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldTPTYP, txtTPTYP, "ITP", " TPTYP='" & txtTPTYP.Text & "'") Then
					MsgBox("This code is already exist")
					txtTPTYP.Focus()
				Else
					txtTPDSC.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace